<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="区域名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入区域名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="负责人名称" prop="managerName" label-width="90px">
        <el-input v-model="queryParams.managerName" placeholder="请输入负责人名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="activeStatus">
        <el-select v-model="queryParams.activeStatus" placeholder="请选择是否启用，0草稿,1禁用，2启用" clearable>
          <el-option v-for="dict in dict.type.status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:region:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-if="false" type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:region:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="regionList" @selection-change="handleSelectionChange">
      <el-table-column label="城市编码" align="center" prop="cityCode" />
      <el-table-column label="区域名称" align="center" prop="name" />
      <el-table-column label="负责人名称" align="center" prop="managerName" />
      <el-table-column label="负责人电话" align="center" prop="managerPhone" />
      <el-table-column label="状态" align="center" prop="activeStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.status" :value="scope.row.activeStatus" />
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:region:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:region:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改区域对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="城市编码" prop="cityCode">
          <el-input v-model="form.cityCode" placeholder="请输入城市编码" />
        </el-form-item>
        <el-form-item label="区域名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入区域名称" />
        </el-form-item>
        <el-form-item label="负责人名称" prop="managerName">
          <el-input v-model="form.managerName" placeholder="请输入负责人名称" />
        </el-form-item>
        <el-form-item label="负责人电话" prop="managerPhone">
          <el-input v-model="form.managerPhone" placeholder="请输入负责人电话" />
        </el-form-item>
        <el-form-item label="状态" prop="activeStatus">
          <el-radio-group v-model="form.activeStatus">
            <el-radio v-for="dict in dict.type.status" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序字段" prop="sortNum">
          <el-input v-model="form.sortNum" placeholder="请输入排序字段" />
        </el-form-item>
        <el-divider content-position="center">区域业务配置信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddConfigRegion">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteConfigRegion">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="configRegionList" :row-class-name="rowConfigRegionIndex"
          @selection-change="handleConfigRegionSelectionChange" ref="configRegion">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50" />
          <el-table-column label="" prop="staffReceiveOrderMax" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.staffReceiveOrderMax" placeholder="请输入" />
            </template>
          </el-table-column>
          <el-table-column label="" prop="institutionReceiveOrderMax" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.institutionReceiveOrderMax" placeholder="请输入" />
            </template>
          </el-table-column>
          <el-table-column label="" prop="staffServeRadius" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.staffServeRadius" placeholder="请输入" />
            </template>
          </el-table-column>
          <el-table-column label="" prop="institutionServeRadius" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.institutionServeRadius" placeholder="请输入" />
            </template>
          </el-table-column>
          <el-table-column label="分流间隔" prop="diversionInterval" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.diversionInterval" placeholder="请输入分流间隔" />
            </template>
          </el-table-column>
          <el-table-column label="抢单超时时间间隔" prop="seizeTimeoutInterval" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.seizeTimeoutInterval" placeholder="请输入抢单超时时间间隔" />
            </template>
          </el-table-column>
          <el-table-column label="派单策略，1：距离优先策略，2：评分优先策略，3：接单量优先策略" prop="dispatchStrategy" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.dispatchStrategy" placeholder="请输入派单策略，1：距离优先策略，2：评分优先策略，3：接单量优先策略" />
            </template>
          </el-table-column>
          <el-table-column label="派单每轮时间间隔" prop="dispatchPerRoundInterval" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.dispatchPerRoundInterval" placeholder="请输入派单每轮时间间隔" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addRegion, delRegion, getRegion, listRegion, updateRegion } from "@/api/system/region";

export default {
  name: "Region",
  dicts: ['status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedConfigRegion: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 区域表格数据
      regionList: [],
      // 区域业务配置表格数据
      configRegionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        managerName: null,
        activeStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cityCode: [
          { required: true, message: "城市编码不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "区域名称不能为空", trigger: "blur" }
        ],
        managerName: [
          { required: true, message: "负责人名称不能为空", trigger: "blur" }
        ],
        managerPhone: [
          { required: true, message: "负责人电话不能为空", trigger: "blur" }
        ],
        activeStatus: [
          { required: true, message: "是否启用，0草稿,1禁用，2启用不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询区域列表 */
    getList() {
      this.loading = true;
      listRegion(this.queryParams).then(response => {
        this.regionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cityCode: null,
        name: null,
        managerName: null,
        managerPhone: null,
        activeStatus: null,
        sortNum: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null
      };
      this.configRegionList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加区域";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRegion(id).then(response => {
        this.form = response.data;
        this.configRegionList = response.data.configRegionList;
        this.open = true;
        this.title = "修改区域";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.configRegionList = this.configRegionList;
          if (this.form.id != null) {
            updateRegion(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRegion(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除区域编号为"' + ids + '"的数据项？').then(function () {
        return delRegion(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 区域业务配置序号 */
    rowConfigRegionIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 区域业务配置添加按钮操作 */
    handleAddConfigRegion() {
      let obj = {};
      obj.staffReceiveOrderMax = "";
      obj.institutionReceiveOrderMax = "";
      obj.staffServeRadius = "";
      obj.institutionServeRadius = "";
      obj.diversionInterval = "";
      obj.seizeTimeoutInterval = "";
      obj.dispatchStrategy = "";
      obj.dispatchPerRoundInterval = "";
      this.configRegionList.push(obj);
    },
    /** 区域业务配置删除按钮操作 */
    handleDeleteConfigRegion() {
      if (this.checkedConfigRegion.length == 0) {
        this.$modal.msgError("请先选择要删除的区域业务配置数据");
      } else {
        const configRegionList = this.configRegionList;
        const checkedConfigRegion = this.checkedConfigRegion;
        this.configRegionList = configRegionList.filter(function (item) {
          return checkedConfigRegion.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleConfigRegionSelectionChange(selection) {
      this.checkedConfigRegion = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/region/export', {
        ...this.queryParams
      }, `region_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
